/*测试数据*/
var usertestdata=[
	{
		id: 1,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 2,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	}
];
var protestdata=[
	{
		id: 1,
		productID: 2322,
		productName: "18817802281",
		productBrand: 'linda',
		productVersion: 'V3高规',
		store: 3455,
		singlePrice: 234,
		tradeCnt: 2234,
		createTime: 1502937009
	},
	{
		id: 2,
		productID: 334,
		productName: "18817802281",
		productBrand: 'linda',
		productVersion: 'V3高规',
		store: 3455,
		singlePrice: 234,
		tradeCnt: 2234,
		createTime: 1502937009
	}
];


var isKF=true;
$(function(){
    getSupplierData();
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
		getProductDataMes();
	}
	$("#productSearch").on('click',function(){
		searchProductData();
	});
	$("#productKeys").bind("keydown", function (e) {
		var key = e.which;
		if (key == 13){
			searchProductData();
		}
	});


    $("#clearProductSearch").on('click',function(){
        $("#proList").empty();
    });

	$("#btnCreateOrder").on('click',function(){
		createOrder();
	});
});
/*获取供应商数据并填充*/
function getSupplierData(){
    $.post(
        gobalServer+"supplier/selectAll",
        {},
        function(backData){
            $("#brandSelect").empty();
            /*var option = $("<option>").attr({"value":0}).text("品牌查询");
            $("#brandSelect").append(option);*/
            if(backData.statusCode==200){
                $.each(backData.commonList,function (index,item) {
                    var option = $("<option>").attr({"value":item.supplierName}).text(item.supplierName);
                    $("#supplierSelect").append(option);
                })
            }else{
                consoleMes($("#manageContent"),2000,"warning","无供应商数据，你可以添加相关供应商！");//success、info、warning、danger

            }
        }
    );
};
/*选购产品展示*/
function addNewProduct(productId) {
	if(productId!=null && productId!=""){
        $.post(
            gobalServer+"product/selectOne",
            {"productId":productId},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    if(datarow.productId!=null){
                        $("#manageContent").attr({"tgid":datarow.productId});
                        $("#productId").text(datarow.productId);
                    }
                    if(datarow.productName!=null){
                        $("#productName").text(datarow.productName);
                    }
                    if(datarow.productBrand!=null){
                        $("#productBrand").text(datarow.productBrand);
                    }
                    if(datarow.productVersion!=null){
                        $("#productVersion").text(datarow.productVersion);
                    }
                    if(datarow.store!=null){
                        $("#productStore").text(datarow.store);
                    }
                    $("#addStoreProduct").show();
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );
	}


}
/*产品信息展示*/
function productMesShow(datarow){
	if(datarow!=null){
		if(datarow.length==undefined){
			var arr=[];
            arr.push(datarow);
            datarow=arr;
		}
		if(datarow.length>0){
            $("#proList").empty();
            $.each(datarow, function(i,item) {
                var $successtr =$("<tr>").addClass("success product-item");
                /*var $productrd =$("<td>").addClass("productrd");*/
                var $productID =$("<td>").addClass("productID").text(item.productId);
                var $productName =$("<td>").addClass("productName").text(item.productName);
                var $productBrand =$("<td>").addClass("productBrand").text(item.productBrand);
                var $productVersion =$("<td>").addClass("productVersion").text(item.productVersion);
                var $store =$("<td>").addClass("store").text(item.store);
                var $singlePrice =$("<td>").addClass("singlePrice").text(item.singlePrice);
                var $tradeCnt =$("<td>").addClass("tradeCnt").text(item.tradeCnt);

                /*var $radioIndex =$("<input>").addClass("radio-product").attr({'type':'radio','name':'proitem','singlePrice':item.singlePrice,'onclick':'productRadioSeleted(this)'}).val(item.productId);*/
                /*$productrd.append($radioIndex);*/

                var $buyNumtd =$("<td>").addClass("productNum");
                var $buyNum = $("<input>").addClass("num-product").attr({"type":"number","min":0,"max":item.store}).val(1);
                $buyNumtd.append($buyNum);
                var $buyBtntd =$("<td>").addClass("");
                var $buyBtn = $("<input>").addClass("btn btn-primary num-product").attr({"type":"button","onclick":"addNewProduct('"+item.productId+"');"}).val("入库");
                $buyBtntd.append($buyBtn);

                $successtr.append($productID,$productName,$productBrand,$productVersion,$store,$singlePrice,$tradeCnt,$buyBtntd);
                $("#proList").append($successtr);
            });
		}else{
            consoleMes($("#manageContent"),3000,"warning","无相关数据！");//success、info、warning、danger
        }

	}else{
        consoleMes($("#manageContent"),3000,"warning","无相关数据，请重试！");//success、info、warning、danger
    }
};
/*搜索产品时*/
function searchProductData(){
	var productKeys= $("#productKeys").val().trim();
	if(productKeys!=null && productKeys!=""){
		/*var datarow = protestdata;
		productMesShow(datarow);*/
        $.post(
            gobalServer+"product/query",
            {"keyword":productKeys,"containZero":1},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.commonList;
                    productMesShow(datarow);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );
	}else{
		consoleMes($("#manageContent"),3000,"warning","请输入要搜索的关键字");
	}
};
/*从产品查询开单按钮跳转过来*/
function getProductDataMes(){
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
		/*var datarow = protestdata;
		productMesShow(datarow);*/
        $.post(
            gobalServer+"product/selectOne",
            {"productId":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    productMesShow(datarow);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}
};

/*开单*/
function createOrder(){
    var productId =  $("#manageContent").attr("tgid");
	var storeAddNum=$("#storeAddNum").val().trim();
    var supplierSelect=$("#supplierSelect").val().trim();
    var storeAddPrice=$("#storeAddPrice").val().trim();
    if(productId==null || productId==""){
        consoleMes($("#manageContent"),2000,"warning","请选择入库产品！");
        return;
    }
	if(storeAddNum==null || storeAddNum=="" || storeAddNum<=0){
		consoleMes($("#manageContent"),2000,"warning","请输入入库数量！");
		return;
	}
	if(supplierSelect==null || supplierSelect==""){
		consoleMes($("#manageContent"),2000,"warning","请选择供应商！");
		return;
	}
    if(storeAddPrice==null || storeAddPrice=="" || storeAddPrice<=0){
        consoleMes($("#manageContent"),2000,"warning","请输入入库价格！");
        return;
    }
	/*consoleMes($("#manageContent"),2000,"success","入库成功！");//success、info、warning、danger
	setTimeout(function () {
        window.location.href=gobalWeb+"product/storeshow.html?tgid="+productId;
    }, 2500);*/
    $.post(
        gobalServer+"instock/insert",
        {"productId":productId,"addNumber":storeAddNum,"inStockPrice":storeAddPrice,"supplier":supplierSelect},
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","入库成功！");//success、info、warning、danger
                setTimeout(function () {
                    window.location.href=gobalWeb+"product/storeshow.html?tgid="+productId;
                }, 2500);
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );

		
};
